//
//  TJLoadingView.m
//  BaoDian
//
//  Created by mac on 2024/8/4.
//

#import "TJLoadingView.h"
#import "TJLoadingView.h"

@interface TJLoadingView()

@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, assign, getter=isAnimating) BOOL animating;


@end


@implementation TJLoadingView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.hidesWhenStopped = YES;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"icon_loading"];
        self.imageView = imageView;
        [self addSubview:imageView];
    }
    return self;
}
 
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _imageView.frame = self.bounds;
}

- (void)startAnimating {
    if (self.animating) return;
    self.animating = YES;
    CABasicAnimation *rotationAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnim.toValue = [NSNumber numberWithFloat:2 * M_PI];
    rotationAnim.duration = 0.6;
    rotationAnim.repeatCount = CGFLOAT_MAX;
    rotationAnim.removedOnCompletion = NO;
    [self.imageView.layer addAnimation:rotationAnim forKey:@"rotation"];
    if (self.hidesWhenStopped) {
        self.hidden = NO;
    }
}

- (void)stopAnimating {
    if (!self.animating) return;
    self.animating = NO;
    [self.imageView.layer removeAllAnimations];
    if (self.hidesWhenStopped) {
        self.hidden = YES;
    }
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(40, 40);
}


@end
